package com.ruoyi.project.wechatprogram.menu.service.impl;

import java.util.List;

import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.wechatprogram.categorymenu.mapper.WxCategoryMenuMapper;
import com.ruoyi.project.wechatprogram.weekend.mapper.WxWeekendMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.wechatprogram.menu.mapper.WxMenuMapper;
import com.ruoyi.project.wechatprogram.menu.domain.WxMenu;
import com.ruoyi.project.wechatprogram.menu.service.IWxMenuService;
import com.ruoyi.common.utils.text.Convert;
import org.springframework.transaction.annotation.Transactional;

/**
 * 菜单列Service业务层处理
 * 
 * @author csx
 * @date 2020-03-27
 */
@Service
public class WxMenuServiceImpl implements IWxMenuService 
{
    private static final Logger log = LoggerFactory.getLogger(WxMenuServiceImpl.class);

    @Autowired
    private WxMenuMapper wxMenuMapper;

    @Autowired
    private WxCategoryMenuMapper wxCategoryMenuMapper;

    @Autowired
    private WxWeekendMapper wxWeekendMapper;

    /**
     * 查询菜单列
     * 
     * @param id 菜单列ID
     * @return 菜单列
     */
    @Override
    public WxMenu selectWxMenuById(Integer id)
    {
        return wxMenuMapper.selectWxMenuById(id);
    }

    /**
     * 查询菜单列列表
     * 
     * @param wxMenu 菜单列
     * @return 菜单列
     */
    @Override
    public List<WxMenu> selectWxMenuList(WxMenu wxMenu)
    {
        return wxMenuMapper.selectWxMenuList(wxMenu);
    }

    /**
     * 查询菜单列列表
     *
     * @param categoryId
     * @return 菜单列
     */
    @Override
    public List<WxMenu> selectWxMenuListByCategoryId(Integer categoryId)
    {
        return wxMenuMapper.selectWxMenuListByCategoryId(categoryId);
    }

    /**
     * 新增菜单列
     * 
     * @param wxMenu 菜单列
     * @return 结果
     */
    @Override
    public int insertWxMenu(WxMenu wxMenu)
    {
        wxMenu.setCreateTime(DateUtils.getNowDate());
        return wxMenuMapper.insertWxMenu(wxMenu);
    }

    /**
     * 修改菜单列
     * 
     * @param wxMenu 菜单列
     * @return 结果
     */
    @Override
    public int updateWxMenu(WxMenu wxMenu)
    {
        wxMenu.setUpdateTime(DateUtils.getNowDate());
        return wxMenuMapper.updateWxMenu(wxMenu);
    }

    /**
     * 修改菜单列，新方法
     *
     * @param wxMenu 菜单列
     * @return 结果
     */
    @Override
    public AjaxResult updateWxMenuNew(WxMenu wxMenu)
    {
        try {
            // 删除菜单，级联删除餐品类型（早中晚）绑定的餐品和每日菜单绑定的餐品
            // 第一步，删除餐品类型（早中晚）绑定的餐品
            wxCategoryMenuMapper.deleteWxCategoryMenuByMenuId(wxMenu.getId());
            // 第二步，删除每日菜单绑定的餐品
            wxWeekendMapper.deleteWxWeekendByMenuId(wxMenu.getId());
            // 更新菜单
            wxMenu.setUpdateTime(DateUtils.getNowDate());
            if (wxMenuMapper.updateWxMenu(wxMenu) <= 0) {
                return AjaxResult.error("菜品下架失败！");
            }
        } catch (Exception e) {
            log.error("操作异常！", e);
            throw new BusinessException("操作异常！");
        }
        return AjaxResult.success("餐品下架成功！");
    }

    /**
     * 删除菜单列对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    @Transactional
    public AjaxResult deleteWxMenuByIds(String ids)
    {
        try {
            // 判断该餐品是否是下架状态，若是下架状态允许删除
            WxMenu wxMenu = wxMenuMapper.selectWxMenuById(Integer.parseInt(ids));
            if (wxMenu.getMenuStatus().equals(WxMenu.MENU_STATUS_UP)) {
                return AjaxResult.error("请先下架该餐品！");
            }
            // 删除菜单
            if (wxMenuMapper.deleteWxMenuById(Integer.parseInt(ids)) <= 0) {
                return AjaxResult.error("删除菜单失败！");
            }
        } catch (Exception e) {
            log.error("操作异常！", e);
            throw new BusinessException("操作异常！");
        }
        return AjaxResult.success();
    }

    /**
     * 批量上架菜品
     * @param ids
     * @return 结果
     */
    @Override
    public int batchUpMenuByIds(String ids) {
        return wxMenuMapper.batchUpMenuByIds(Convert.toStrArray(ids));
    }

    /**
     * 批量下架菜品
     * @param ids
     * @return 结果
     */
    @Override
    public int batchDownMenuByIds(String ids) {
        return wxMenuMapper.batchDownMenuByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除菜单列信息
     * 
     * @param id 菜单列ID
     * @return 结果
     */
    @Override
    public int deleteWxMenuById(Integer id)
    {
        return wxMenuMapper.deleteWxMenuById(id);
    }
}
